//个人信息
var userdata = function (data) {
    $.post({
        url: './UserData',
        success: function (data2) {
            if (data2.code == 0) {
                data["userdata"] = data2.result
                collections(data)
            }
            else {
                collections(data)
            }
        }
    })
}

let collections_ = function (data) {
    data.data.forEach(element => {
        let button_class = "collection btn btn-sm btn-outline-secondary"
        $('#container').html($('#container').html() + `
    <div class="col shadow p-3 mb-5 bg-body rounded">
        <div class="card shadow-sm">
            <img src="`+ element.Samplefigure + `" alt="Placeholder" class="Food  bd-placeholder-img card-img-top"
                width="100%" height="225">
            <div class="card-body">
                <p class="card-text">`+ element.FoodName + `</p>
                <div class="d-flex justify-content-between align-items-center">
                    <div class="btn-group">
                        <button type="button" class="`+ button_class + `" data-FoodID="` + element.FoodID + `"><a href="./html/login">未登录</a></button>
                    </div>
                    <small class="text-muted">`+ element.Release_time.substr(0, 10) + `</small>
                </div>
            </div>
        </div>
    </div>
    `)
    });
}

//是否收藏
const collections = function (data) {
    if (document.cookie.length != 20) {
        $.post({
            url: './collection/select',
            success: function (data1) {
                if (data1.code == 0) {
                    let button_class = " collection btn btn-sm btn-outline-secondary"
                    let uncollection = 'uncollection'
                    let boot
                    data.data.forEach(element => {
                        uncollection = 'uncollection'
                        boot = `<button type="button" class="` + button_class + `" data-FoodID="` + element.FoodID + `">收藏</button>`
                        for (let i = 0; i < data1.data.length; i++) {//是否在收藏列表   
                            if (data1.data[i].FoodID == element.FoodID) {
                                button_class = "collection btn btn-sm btn-outline-secondary active"
                                uncollection = ''
                                break
                            }
                            else {
                                button_class = " collection btn btn-sm btn-outline-secondary"
                            }
                        }
                        boot = `<button type="button" class="` + button_class + `" data-FoodID="` + element.FoodID + `">收藏</button>`

                        //是否是自己发布的
                        if (data.userdata && data.userdata.UserID == element.UserID) {
                            boot = `<button class="uncollection btn btn-sm btn-outline-secondary" data-FoodID="` + element.FoodID + `">我发布的</button>`
                        }

                        //页面数据
                        $('#container').html($('#container').html() + `
                <div class=" `+ uncollection + ` col shadow p-3 mb-5 bg-body rounded hidden" >
                    <div class="card shadow-sm">
                        <img src="`+ element.Samplefigure + `" alt="Placeholder" class="Food bd-placeholder-img card-img-top"
                            width="100%" height="225">
                        <div class="card-body">
                            <p class="card-text">`+ element.FoodName + `</p>
                            <div class="d-flex justify-content-between align-items-center">
                                <div class="btn-group">
                                    `+ boot + `
                                </div>
                                <small class="text-muted">`+ element.Release_time.substr(0, 10) + `</small>
                            </div>
                        </div>
                    </div>
                </div>
                `)
                    });
                    if ($('#mycollections').attr('class') == 'btn btn-primary btn-sm')
                        $('.uncollection').hide()
                } else if (data1.code == -1) {
                    collections_(data)
                }

            }
        })
    } else {
        console.log(data);
        collections_(data)
    }
}

//寄存第一次布局的数据，避免重复请求
var recipes

// 页面加载
$.ajax({
    url: './recipes',
    type: 'POST',
    data: {},
    success: function (data) {
        recipes = data
        if (data.data[0])
            userdata(data)

    },
    error: function () {
        alert(12)
    }
})

//搜索
$("#Search").on('click', function () {
    $.post({
        url: './recipes',
        data: { FoodName: $("#searchinput").val() },
        success: function (data) {
            $('#container').html('')
            if (data.data[0])
                collections(data)
        }
    })
})

//退出登录
$("#Exit").on('click', function () {
    $.ajax({
        url: './Exit',
        type: 'POST',
        success: function () {
            window.location.href = './'
        }
    })
})

//收藏
$("body").on('click', ".collection", function () {
    var ys = $(this)
    var url;
    var button__class;
    if (ys.attr("class") != 'collection btn btn-sm btn-outline-secondary active') {
        url = './collection/insert'
        button__class = "collection btn btn-sm btn-outline-secondary active"
        ys.parent().parent().parent().parent().parent().attr('class', 'col shadow p-3 mb-5 bg-body rounded hidden')

    } else {
        url = './collection/delete'
        button__class = "collection btn btn-sm btn-outline-secondary"
        ys.parent().parent().parent().parent().parent().attr('class', 'uncollection col shadow p-3 mb-5 bg-body rounded hidden')
    } $.post({
        url: url,
        data: { FoodID: $(this).data().foodid },
        success: function (data) {
            if (data.code == 0) {
                ys.attr("class", button__class);
            }
        }
    })
    if ($('#mycollections').attr('class') == 'btn btn-primary btn-sm')
        $('.uncollection').hide()
})

let cuisines

//菜系
$.post({
    url: './class',
    success: function (data) {
        // console.log(data);
        var data_ = data
        var cuisine = []//收集分类
        let cuisine_N//记录编号
        let index = 0//分类下标
        while (data.data.length) {
            len = 0
            cuisine_N = data_.data[0].series
            cuisine[index] = new Array();
            for (let i = 0; i < data_.data.length; i++) {
                if (data_.data[i].series == cuisine_N) {
                    cuisine[index][cuisine[index].length] = data_.data[i]

                }
            }
            for (let j = 0; j < cuisine[index].length; j++) {
                for (let i = 0; i < data_.data.length; i++) {
                    if (data_.data[i] == cuisine[index][j])
                        data_.data.splice(i, 1)
                }
            }
            index++
        }
        let listName
        let elements = ''
        cuisines = cuisine
        $.each(cuisine, function (index, value) {
            elements = ''
            $.each(this, function (index_, value_) {
                elements += '<li><a class="cuisine dropdown-item" href="#" data-classid="' + value_.ClassID + '">' + value_.ClassName + '</a></li>'
                listName = value_.cuisine
            })
            $(".chooses").html($(".chooses").html() + `
                        <div class="dropdown d-inline-block me-2">
                    <button class="cuisine_ btn btn-secondary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown"
                        aria-expanded="false" data-classid="">
                        `+ listName + `
                    </button>
                    <ul class="dropdown-menu">
                        <li>
                            <h6 class="dropdown-header">选一个喜欢的菜系</h6>
                        </li>
                        `+ elements + `
                        <li>
                            <hr class="dropdown-divider">
                        </li>
                        <li><a class="cuisine dropdown-item" data-classid="" href="#">`+ listName + `</a></li>
                    </ul>
                </div>
                        `)
        })
    }
})

//分页

//开启收藏阅览
$('body').on('click', '#mycollections', function () {
    if ($(this).attr('class') != 'btn btn-primary btn-sm') {
        $(this).attr('class', 'btn btn-primary btn-sm')

    } else {
        $(this).attr('class', ' btn btn-secondary btn-sm')
    }
    let new_recipe = { data: [] };
    $.each($('.cuisine_'), function (index, value) {
        for (let i = 0; i < recipes.data.length; i++) {
            if (recipes.data[i].ClassID == $(value).prop('data-classid')) {
                new_recipe.data[new_recipe.data.length] = recipes.data[i]
            }
        }
    })
    $('#container').html('')
    if (!new_recipe.data[0]) {
        new_recipe.data = recipes.data
    }
    userdata(new_recipe)
})

//菜系选择
$('body').on('click', '.cuisine', function () {
    let new_recipe = { data: [] };
    $(this).parent().parent().prev().text($(this).text())
    $(this).parent().parent().prev().prop('data-classid', $(this).data().classid)
    new_recipe = { data: [] };
    $.each($('.cuisine_'), function (index, value) {
        if (!$(value).prop('data-classid')) {
            $.each(cuisines[index], function (index_, value_) {
                for (let i = 0; i < recipes.data.length; i++) {
                    if (recipes.data[i].ClassID == value_.ClassID) {
                        new_recipe.data[new_recipe.data.length] = recipes.data[i]
                    }
                }
            })
        }
        else
            for (let i = 0; i < recipes.data.length; i++) {
                if (recipes.data[i].ClassID == $(value).prop('data-classid')) {
                    new_recipe.data[new_recipe.data.length] = recipes.data[i]
                }
            }

    })
    $('#container').html('')
    userdata(new_recipe)
})

//菜品详情
$('body').on('click', '.Food', function () {
    let FoodID = $(this).next().children().eq(1).children().children().attr("data-foodid")
    window.location.href = '/details/' + FoodID
})
